package dao;

import pojo.Card;
import utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author konley
 * @date 2020-06-10 15:32
 */
public class CardDao {
    //根据sid查饭卡
    public Card getCard(int sid){
        Card card = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = DBUtils.getConnection();
        try {
            ps = con.prepareStatement("select * from card where sid=?");
            ps.setString(1,sid+"");
            rs = ps.executeQuery();
            if(rs.next()){
                card = new Card();
                card.setCid(rs.getInt("cid"));
                card.setSid(rs.getInt("sid"));
                card.setMoney(rs.getInt("money"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.release(rs,ps,con);
        }
        return card;
    }
    //根据cid查饭卡
    public Card getCardByCid(int cid){
        Card card = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = DBUtils.getConnection();
        try {
            ps = con.prepareStatement("select * from card where cid=?");
            ps.setString(1,cid+"");
            rs = ps.executeQuery();
            if(rs.next()){
                card = new Card();
                card.setCid(rs.getInt("cid"));
                card.setSid(rs.getInt("sid"));
                card.setMoney(rs.getInt("money"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.release(rs,ps,con);
        }
        return card;
    }
    //根据sid查cid
    public int getCid(int sid){
        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection con = DBUtils.getConnection();
        try {
            ps = con.prepareStatement("select cid from card where sid=?");
            ps.setInt(1,sid);
            rs = ps.executeQuery();
            if(rs.next()){
                return rs.getInt("cid");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.release(rs,ps,con);
        }
        return 0;
    }
    //根据cid查sid
    public int getSid(int cid){
        ResultSet rs = null;
        PreparedStatement ps = null;
        Connection con = DBUtils.getConnection();
        try {
            ps = con.prepareStatement("select sid from card where cid=?");
            ps.setInt(1,cid);
            rs = ps.executeQuery();
            if(rs.next()){
                return rs.getInt("sid");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.release(rs,ps,con);
        }
        return 0;
    }

    //添加饭卡
    public boolean cardAdd(int sid,int money){
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = DBUtils.getConnection();
        try{
            ps = con.prepareStatement("insert into card value(null,?,?)");
            ps.setInt(1,sid);
            ps.setInt(2,money); //首次开卡充值的钱
            int num = ps.executeUpdate();
            if(num > 0){
                return true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.release(ps,con);
        }
        return false;
    }

    //充值饭卡
    public boolean cardUpdate(int cid,int addMoney){
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = DBUtils.getConnection();
        //找到饭卡
        Card card = new CardDao().getCardByCid(cid);
        //增加余额
        int money = card.getMoney();
        int moneyNow = money+addMoney;
        try{
            ps = con.prepareStatement("update card set money=? where cid=?");
            ps.setInt(1,moneyNow);
            ps.setInt(2,cid);
            int num = ps.executeUpdate();
            if(num > 0){
                return true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.release(ps,con);
        }
        return false;
    }

    //删除饭卡
    public boolean deleteCard(int cid){
        PreparedStatement ps = null;
        Connection con = DBUtils.getConnection();
        try{
            ps = con.prepareStatement("delete  from card where cid=?");
            ps.setInt(1,cid);
            int num = ps.executeUpdate();
            if(num > 0){
                return true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBUtils.release(ps,con);
        }
        return false;
    }
}
